package com.fortune.service.impl;

import com.fortune.dao.COrderBaseDao;
import com.fortune.dto.in.cOrderMgt.COrderAddDto;
import com.fortune.dto.in.cOrderMgt.COrderEditDto;
import com.fortune.dto.in.cOrderMgt.COrderListDto;
import com.fortune.service.COrderBaseService;
import com.fortune.vo.cOrderMgt.COrder;
import com.fortune.vo.cOrderMgt.COrderBiller;
import com.fortune.vo.cOrderMgt.COrderDetails;
import com.fortune.vo.cOrderMgt.COrderDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
public class COrderBaseServiceImpl implements COrderBaseService {
    @Autowired
    private COrderBaseDao cOrderBaseDao;

    /**
     * 查询客户订单列表
     * @param companyId
     * @param dealerId
     * @param listDto
     * @return
     */
    @Override
    public List<COrder> queryOrderBaseList(String companyId, String dealerId, COrderListDto listDto) {
        return cOrderBaseDao.queryOrderBaseList(companyId,dealerId,listDto);
    }

    /**
     * 查询客户订单列表个数
     * @param companyId
     * @param dealerId
     * @param listDto
     * @return
     */
    @Override
    public int queryOrderBaseListCount(String companyId, String dealerId, COrderListDto listDto) {
        return cOrderBaseDao.queryOrderBaseListCount(companyId,dealerId,listDto);
    }

    /**
     * 添加客户订单
     * @param companyId
     * @param dealerId
     * @param addDto
     * @return
     */
    @Override
    @Transactional
    public int addOrderBase(String companyId, String dealerId, COrderAddDto addDto) {
        //订单id
        String id = UUID.randomUUID().toString().replace("-","");
        int num = cOrderBaseDao.addOrderBase(id,companyId,dealerId,addDto);
        if (num < 1){
            throw new RuntimeException("添加客户订单失败");
        }
        num = cOrderBaseDao.addOrderBaseDetails(addDto.getId(),id,companyId,dealerId,addDto.getProduce());
        if (num < 1){
            throw new RuntimeException("添加客户订单失败");
        }
        return 1;
    }

    /**
     * 编辑客户订单
     * @param companyId
     * @param dealerId
     * @param editDto
     * @return
     */
    @Override
    @Transactional
    public int editOrderBase(String companyId, String dealerId, COrderEditDto editDto) {
        int num = cOrderBaseDao.editOrderBase(companyId,dealerId,editDto);
        if (num < 1){
            throw new RuntimeException("编辑客户订单失败");
        }
        num = cOrderBaseDao.deleteOrderBase(companyId,dealerId,editDto.getId(),editDto.getOrdersId());
        if (num < 1){
            throw new RuntimeException("编辑客户订单失败");
        }
        num = cOrderBaseDao.addOrderBaseDetails(editDto.getId(),editDto.getOrdersId(),companyId,dealerId,editDto.getProduce());
        if (num < 1){
            throw new RuntimeException("编辑客户订单失败");
        }
        return 1;
    }

    /**
     * 删除客户订单
     * @param companyId
     * @param dealerId
     * @param orderId
     * @param clientId
     * @return
     */
    @Override
    public int deleteOrderBase(String companyId, String dealerId, String orderId, String clientId) {
        return cOrderBaseDao.deleteOrder(companyId,dealerId,orderId,clientId);
    }

    /**
     * 批准发货
     * @param companyId
     * @param dealerId
     * @param orderId
     * @param clientId
     * @return
     */
    @Override
    public int updateApproved(String companyId, String dealerId, String orderId, String clientId) {
        return cOrderBaseDao.updateApproved(companyId,dealerId,orderId,clientId);
    }

    /**
     * 详情
     * @param companyId
     * @param dealerId
     * @param orderId
     * @param clientId
     * @return
     */
    @Override
    public COrderDetails queryDetails(String companyId, String dealerId, String orderId, String clientId) {
        return cOrderBaseDao.queryDetails(companyId,dealerId,orderId,clientId);
    }

    /**
     * 查询经销商司机
     * @param companyId
     * @param dealerId
     * @param driverId
     * @return
     */
    @Override
    public List<COrderDriver> queryDriverList(String companyId, String dealerId, String driverId) {
        return cOrderBaseDao.queryDriverList(companyId,dealerId,driverId);
    }

    /**
     * 开单人信息
     * @param companyId
     * @param dealerId
     * @param userId
     * @return
     */
    @Override
    public COrderBiller queryBiller(String companyId, String dealerId, String userId) {
        return cOrderBaseDao.queryBiller(companyId,dealerId,userId);
    }


}
